package com.eatme.web.app.article.service.impl;

import com.eatme.web.common.QueryCondition;
import com.eatme.web.common.QueryResult;
import com.eatme.web.common.vo.AppResponse;
import com.eatme.web.app.article.service.SysUserService;
import com.eatme.web.sys.entity.SysUser;
import com.eatme.web.sys.repository.SysUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.domain.jaxb.SpringDataJaxb;
import org.springframework.stereotype.Service;

import java.util.*;

@Service("userServiceImpl")
public class UserServiceImpl implements SysUserService {
    @Autowired
    SysUserRepository sysUserRepository;
    @Override
    public AppResponse addUser(com.eatme.web.sys.entity.SysUser sysUser) {
        //int i = sysUserDao.addUser(user);
        sysUser = sysUserRepository.save(sysUser);
        return AppResponse.ok();
    }

    @Override
    public com.eatme.web.sys.entity.SysUser findByUserName(String userName) {
        com.eatme.web.sys.entity.SysUser sysUser = sysUserRepository.findSysUserByUsername(userName);
        return sysUser;
    }

    @Override
    public Map findList(QueryCondition<com.eatme.web.sys.entity.SysUser> queryCondition) {
        com.eatme.web.sys.entity.SysUser sysUser = queryCondition.getCondition();
        Map<String,Object> map = new HashMap<>();
        if (sysUser!=null) {
            ExampleMatcher matcher = ExampleMatcher.matching();
            Example<com.eatme.web.sys.entity.SysUser> example = Example.of(sysUser,matcher);
            List<Sort.Order> orders = new ArrayList<Sort.Order>();
            orders.add(new Sort.Order(Sort.Direction.DESC,"id"));
            Pageable pageable = PageRequest.of((queryCondition.getPage().getCurrentPage()-1)*queryCondition.getPage().getPageSize(),queryCondition.getPage().getPageSize());
            Page<com.eatme.web.sys.entity.SysUser> sysUserPage = sysUserRepository.findAll(example,pageable);
            map.put("list",sysUserPage.getContent());
            map.put("total",sysUserPage.getTotalElements());
        }else {
            Pageable pageable = PageRequest.of((queryCondition.getPage().getCurrentPage()-1)*queryCondition.getPage().getPageSize(),queryCondition.getPage().getPageSize());
            Page<com.eatme.web.sys.entity.SysUser> sysUserPage = sysUserRepository.findAll(pageable);
            map.put("list",sysUserPage.getContent());
            map.put("total",sysUserPage.getTotalElements());
        }

        return map;
    }

    @Override
    public com.eatme.web.sys.entity.SysUser detail(Integer id) {
        com.eatme.web.sys.entity.SysUser sysUser = sysUserRepository.findById(id).get();
        return sysUser;
    }

    @Override
    public com.eatme.web.sys.entity.SysUser findRole(Integer id) {
        com.eatme.web.sys.entity.SysUser sysUser = sysUserRepository.findById(id).get();
        return sysUser;
    }

    @Override
    public AppResponse updateUserRole(SysUser user) {
        sysUserRepository.save(user);
        return AppResponse.ok();
    }

    @Override
    public AppResponse delete(Integer id) {
        int i = sysUserRepository.updateSysUserStatusById(id);
        return AppResponse.ok();
    }

    @Override
    public AppResponse query(QueryCondition<com.eatme.web.sys.entity.SysUser> queryCondition) {
        QueryResult result = null;
        com.eatme.web.sys.entity.SysUser sysUser = queryCondition.getCondition();
        Map<String,Object> map = new HashMap<>();
        if (sysUser!=null) {
            ExampleMatcher matcher = ExampleMatcher.matching();
            Example<com.eatme.web.sys.entity.SysUser> example = Example.of(sysUser,matcher);
            List<Sort.Order> orders = new ArrayList<Sort.Order>();
            orders.add(new Sort.Order(Sort.Direction.DESC,"id"));
            Pageable pageable = PageRequest.of(queryCondition.getPage().getCurrentPage(),queryCondition.getPage().getPageSize());
            Page<com.eatme.web.sys.entity.SysUser> page = sysUserRepository.findAll(example,pageable);
            result = new QueryResult<>(page.getTotalElements(),page.getContent());
        }else {
            Pageable pageable = PageRequest.of(0,10);
            Page<com.eatme.web.sys.entity.SysUser> page = sysUserRepository.findAll(pageable);
            result = new QueryResult<>(page.getTotalElements(),page.getContent());
        }

        return AppResponse.ok().data(result);
    }
}
